{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T08:03:33.597551Z",
     "start_time": "2020-11-17T08:03:32.895832Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=2)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "from collections import Counter\n",
    "\n",
    "sns.set_style('ticks')\n",
    "\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['figure.dpi']= 300\n",
    "mpl.rc(\"savefig\", dpi=300)\n",
    "\n",
    "from scipy.special import xlogy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Read files and select drugs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:51:54.158573Z",
     "start_time": "2020-07-11T15:51:54.155430Z"
    }
   },
   "outputs": [],
   "source": [
    "# log2_median_ic50, log2_median_ic50_9f, log2_median_ic50_hn, log2_median_ic50_9f_hn, log2_median_ic50_3f_hn, log2_max_conc\n",
    "ref_type = 'log2_median_ic50_hn' # log2_median_ic50_3f_hn | log2_median_ic50_hn\n",
    "model_name = 'hn_drug_cw_dw10_100000_model' # hn_drug_cw_dw10_100000_model | hn_drug_cw_dw1_100000_model | hn_drug_cw_dwsim10_100000_model\n",
    "\n",
    "dosage_shifted = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:04.968759Z",
     "start_time": "2020-07-11T15:52:04.965745Z"
    }
   },
   "outputs": [],
   "source": [
    "norm_type = 'cell_TPM'\n",
    "\n",
    "current_dir = '../result/HN_model/{}/'.format(norm_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:05.142269Z",
     "start_time": "2020-07-11T15:52:05.111712Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Drug Name</th>\n",
       "      <th>Synonyms</th>\n",
       "      <th>Target</th>\n",
       "      <th>Target Pathway</th>\n",
       "      <th>Selleckchem Cat#</th>\n",
       "      <th>CAS number</th>\n",
       "      <th>PubCHEM</th>\n",
       "      <th>Others</th>\n",
       "      <th>entropy</th>\n",
       "      <th>max_conc</th>\n",
       "      <th>...</th>\n",
       "      <th>median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_hn</th>\n",
       "      <th>median_ic50_hn</th>\n",
       "      <th>median_ic50_3f_hn</th>\n",
       "      <th>log2_median_ic50_3f_hn</th>\n",
       "      <th>median_ic50_9f_hn</th>\n",
       "      <th>log2_median_ic50_9f_hn</th>\n",
       "      <th>num_sensitive</th>\n",
       "      <th>num_sensitive_hn</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Drug ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...</td>\n",
       "      <td>AMPK agonist</td>\n",
       "      <td>Metabolism</td>\n",
       "      <td>S1802</td>\n",
       "      <td>2627-69-2</td>\n",
       "      <td>65110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.034272</td>\n",
       "      <td>2000.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>206.748380</td>\n",
       "      <td>7.691732</td>\n",
       "      <td>9.939784</td>\n",
       "      <td>982.139588</td>\n",
       "      <td>327.379863</td>\n",
       "      <td>8.354822</td>\n",
       "      <td>109.126621</td>\n",
       "      <td>6.769859</td>\n",
       "      <td>476</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>Camptothecin</td>\n",
       "      <td>7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...</td>\n",
       "      <td>TOP1</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1288</td>\n",
       "      <td>7689-03-4</td>\n",
       "      <td>104842</td>\n",
       "      <td>(SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...</td>\n",
       "      <td>4.609530</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002003</td>\n",
       "      <td>-8.963413</td>\n",
       "      <td>-7.587491</td>\n",
       "      <td>0.005199</td>\n",
       "      <td>0.001733</td>\n",
       "      <td>-9.172454</td>\n",
       "      <td>0.000578</td>\n",
       "      <td>-10.757416</td>\n",
       "      <td>688</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>Vinblastine</td>\n",
       "      <td>Velban</td>\n",
       "      <td>Microtubule destabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1248</td>\n",
       "      <td>143-67-9</td>\n",
       "      <td>6710780</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.297122</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001599</td>\n",
       "      <td>-9.289051</td>\n",
       "      <td>-7.150982</td>\n",
       "      <td>0.007036</td>\n",
       "      <td>0.002345</td>\n",
       "      <td>-8.735945</td>\n",
       "      <td>0.000782</td>\n",
       "      <td>-10.320907</td>\n",
       "      <td>753</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1006</th>\n",
       "      <td>Cytarabine</td>\n",
       "      <td>Ara-Cytidine, Arabinosyl Cytosine, U-19920</td>\n",
       "      <td>Antimetabolite</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1648</td>\n",
       "      <td>147-94-4</td>\n",
       "      <td>6253</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.646594</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.163032</td>\n",
       "      <td>-2.616771</td>\n",
       "      <td>-1.342632</td>\n",
       "      <td>0.394301</td>\n",
       "      <td>0.131434</td>\n",
       "      <td>-2.927594</td>\n",
       "      <td>0.043811</td>\n",
       "      <td>-4.512557</td>\n",
       "      <td>508</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1007</th>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>RP-56976, Taxotere</td>\n",
       "      <td>Microtubule stabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1148</td>\n",
       "      <td>114977-28-5</td>\n",
       "      <td>148124</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.220984</td>\n",
       "      <td>0.0125</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000761</td>\n",
       "      <td>-10.358915</td>\n",
       "      <td>-9.792998</td>\n",
       "      <td>0.001127</td>\n",
       "      <td>0.000376</td>\n",
       "      <td>-11.377960</td>\n",
       "      <td>0.000125</td>\n",
       "      <td>-12.962923</td>\n",
       "      <td>584</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Drug Name  \\\n",
       "Drug ID                        \n",
       "1001     AICA Ribonucleotide   \n",
       "1003            Camptothecin   \n",
       "1004             Vinblastine   \n",
       "1006              Cytarabine   \n",
       "1007               Docetaxel   \n",
       "\n",
       "                                                  Synonyms  \\\n",
       "Drug ID                                                      \n",
       "1001     AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...   \n",
       "1003     7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...   \n",
       "1004                                                Velban   \n",
       "1006            Ara-Cytidine, Arabinosyl Cytosine, U-19920   \n",
       "1007                                    RP-56976, Taxotere   \n",
       "\n",
       "                           Target   Target Pathway Selleckchem Cat#  \\\n",
       "Drug ID                                                               \n",
       "1001                 AMPK agonist       Metabolism            S1802   \n",
       "1003                         TOP1  DNA replication            S1288   \n",
       "1004     Microtubule destabiliser          Mitosis            S1248   \n",
       "1006               Antimetabolite  DNA replication            S1648   \n",
       "1007       Microtubule stabiliser          Mitosis            S1148   \n",
       "\n",
       "          CAS number  PubCHEM  \\\n",
       "Drug ID                         \n",
       "1001       2627-69-2    65110   \n",
       "1003       7689-03-4   104842   \n",
       "1004        143-67-9  6710780   \n",
       "1006        147-94-4     6253   \n",
       "1007     114977-28-5   148124   \n",
       "\n",
       "                                                    Others   entropy  \\\n",
       "Drug ID                                                                \n",
       "1001                                                   NaN  6.034272   \n",
       "1003     (SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...  4.609530   \n",
       "1004                                                   NaN  4.297122   \n",
       "1006                                                   NaN  6.646594   \n",
       "1007                                                   NaN  4.220984   \n",
       "\n",
       "          max_conc  ...  median_ic50_9f  log2_median_ic50_9f  \\\n",
       "Drug ID             ...                                        \n",
       "1001     2000.0000  ...      206.748380             7.691732   \n",
       "1003        0.1000  ...        0.002003            -8.963413   \n",
       "1004        0.1000  ...        0.001599            -9.289051   \n",
       "1006        2.0000  ...        0.163032            -2.616771   \n",
       "1007        0.0125  ...        0.000761           -10.358915   \n",
       "\n",
       "         log2_median_ic50_hn  median_ic50_hn  median_ic50_3f_hn  \\\n",
       "Drug ID                                                           \n",
       "1001                9.939784      982.139588         327.379863   \n",
       "1003               -7.587491        0.005199           0.001733   \n",
       "1004               -7.150982        0.007036           0.002345   \n",
       "1006               -1.342632        0.394301           0.131434   \n",
       "1007               -9.792998        0.001127           0.000376   \n",
       "\n",
       "         log2_median_ic50_3f_hn  median_ic50_9f_hn  log2_median_ic50_9f_hn  \\\n",
       "Drug ID                                                                      \n",
       "1001                   8.354822         109.126621                6.769859   \n",
       "1003                  -9.172454           0.000578              -10.757416   \n",
       "1004                  -8.735945           0.000782              -10.320907   \n",
       "1006                  -2.927594           0.043811               -4.512557   \n",
       "1007                 -11.377960           0.000125              -12.962923   \n",
       "\n",
       "         num_sensitive  num_sensitive_hn  \n",
       "Drug ID                                   \n",
       "1001               476                27  \n",
       "1003               688                30  \n",
       "1004               753                33  \n",
       "1006               508                25  \n",
       "1007               584                32  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_info_df = pd.read_csv('../preprocessed_data/GDSC/hn_drug_stat.csv', index_col=0)\n",
    "drug_info_df.index = drug_info_df.index.astype(str)\n",
    "\n",
    "drug_id_name_dict = dict(zip(drug_info_df.index, drug_info_df['Drug Name'].values))\n",
    "\n",
    "drug_info_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:05.279485Z",
     "start_time": "2020-07-11T15:52:05.273984Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Afatinib',\n",
       " 'Docetaxel',\n",
       " 'Doxorubicin',\n",
       " 'Epothilone B',\n",
       " 'Gefitinib',\n",
       " 'Obatoclax Mesylate',\n",
       " 'PHA-793887',\n",
       " 'PI-103',\n",
       " 'Vorinostat']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tested_drug_list = [1032, 1007, 133, 201, 1010] + [182, 301, 302] + [1012]\n",
    "[drug_id_name_dict[str(d)] for d in tested_drug_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:05.454150Z",
     "start_time": "2020-07-11T15:52:05.430078Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill</th>\n",
       "      <th>drug_name</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1001</td>\n",
       "      <td>0.941144</td>\n",
       "      <td>34.518773</td>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>1</td>\n",
       "      <td>34.518773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1003</td>\n",
       "      <td>1.660548</td>\n",
       "      <td>26.558972</td>\n",
       "      <td>Camptothecin</td>\n",
       "      <td>1</td>\n",
       "      <td>26.558972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1004</td>\n",
       "      <td>1.438976</td>\n",
       "      <td>28.542245</td>\n",
       "      <td>Vinblastine</td>\n",
       "      <td>1</td>\n",
       "      <td>28.542245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1006</td>\n",
       "      <td>1.629793</td>\n",
       "      <td>26.066463</td>\n",
       "      <td>Cytarabine</td>\n",
       "      <td>1</td>\n",
       "      <td>26.066463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>3.234392</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>10.897336</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id     delta       kill            drug_name  cluster_p  \\\n",
       "0   HN120    1001  0.941144  34.518773  AICA Ribonucleotide          1   \n",
       "1   HN120    1003  1.660548  26.558972         Camptothecin          1   \n",
       "2   HN120    1004  1.438976  28.542245          Vinblastine          1   \n",
       "3   HN120    1006  1.629793  26.066463           Cytarabine          1   \n",
       "4   HN120    1007  3.234392  10.897336            Docetaxel          1   \n",
       "\n",
       "   cluster_kill  \n",
       "0     34.518773  \n",
       "1     26.558972  \n",
       "2     28.542245  \n",
       "3     26.066463  \n",
       "4     10.897336  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if dosage_shifted:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}_shifted.csv'.format(ref_type, model_name))\n",
    "else:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}.csv'.format(ref_type, model_name))\n",
    "\n",
    "\n",
    "single_drug_pred_df.loc[:, 'drug_id'] = single_drug_pred_df.loc[:, 'drug_id'].values.astype(str)\n",
    "single_drug_pred_df.loc[:, 'drug_name'] = [drug_id_name_dict[d] for d in single_drug_pred_df.loc[:, 'drug_id'].values]\n",
    "\n",
    "patient_list = sorted(list(set(single_drug_pred_df['patient'])))\n",
    "# sel_drug_id_list = sorted(list(set(single_drug_pred_df['drug_id'])))\n",
    "\n",
    "single_drug_pred_df.loc[:, 'cluster_p'] = 1\n",
    "single_drug_pred_df.loc[:, 'cluster_kill'] = single_drug_pred_df['kill']\n",
    "\n",
    "single_drug_pred_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List all drug pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:05.751669Z",
     "start_time": "2020-07-11T15:52:05.737992Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(216, 3)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B\n",
       "0   HN120  1032  1007\n",
       "1   HN120  1032   133\n",
       "2   HN120  1032   201\n",
       "3   HN120  1032  1010\n",
       "4   HN120  1032   182"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_list = []\n",
    "n_drugs = len(tested_drug_list)\n",
    "\n",
    "for p in patient_list:\n",
    "    for x in range(0, n_drugs-1):\n",
    "        for y in range(x+1, n_drugs):\n",
    "            drug_x = str(tested_drug_list[x])\n",
    "            drug_y = str(tested_drug_list[y])\n",
    "\n",
    "            drug_combi_list += [[p, drug_x, drug_y]]\n",
    "\n",
    "drug_combi_df = pd.DataFrame(drug_combi_list, columns=['patient', 'A', 'B'])\n",
    "\n",
    "print (drug_combi_df.shape)\n",
    "drug_combi_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Get pred and info for each drug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:06.533250Z",
     "start_time": "2020-07-11T15:52:06.516029Z"
    }
   },
   "outputs": [],
   "source": [
    "merge_df = pd.merge(drug_combi_df, single_drug_pred_df, how='left', left_on=['patient', 'A'], right_on=['patient', 'drug_id'])\n",
    "drug_combi_pred_df = pd.merge(merge_df, single_drug_pred_df[['patient', 'drug_id', 'drug_name', 'cluster_kill', 'kill']], how='left', left_on=['patient', 'B'], right_on=['patient', 'drug_id'], suffixes=['_A', '_B'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:06.896007Z",
     "start_time": "2020-07-11T15:52:06.877909Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1007</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>10.897336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>133</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>59.126943</td>\n",
       "      <td>59.126943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>201</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>39.492534</td>\n",
       "      <td>39.492534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1010</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>16.555392</td>\n",
       "      <td>16.555392</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>182</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>46.369313</td>\n",
       "      <td>46.369313</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     delta     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1032  1007      1032  3.193168  10.719726    Afatinib          1   \n",
       "1   HN120  1032   133      1032  3.193168  10.719726    Afatinib          1   \n",
       "2   HN120  1032   201      1032  3.193168  10.719726    Afatinib          1   \n",
       "3   HN120  1032  1010      1032  3.193168  10.719726    Afatinib          1   \n",
       "4   HN120  1032   182      1032  3.193168  10.719726    Afatinib          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \n",
       "0       10.719726      1007           Docetaxel       10.897336  10.897336  \n",
       "1       10.719726       133         Doxorubicin       59.126943  59.126943  \n",
       "2       10.719726       201        Epothilone B       39.492534  39.492534  \n",
       "3       10.719726      1010           Gefitinib       16.555392  16.555392  \n",
       "4       10.719726       182  Obatoclax Mesylate       46.369313  46.369313  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:09.199699Z",
     "start_time": "2020-07-11T15:52:09.142276Z"
    }
   },
   "outputs": [],
   "source": [
    "rows = []\n",
    "for _, data in drug_combi_pred_df.iterrows():\n",
    "\n",
    "    \n",
    "    cluster_kill_A = data['cluster_kill_A']\n",
    "    cluster_kill_B = data['cluster_kill_B']\n",
    "    cluster_kill_C = cluster_kill_A + cluster_kill_B - np.multiply(cluster_kill_A/100, cluster_kill_B/100)*100\n",
    "    kill_C = cluster_kill_C\n",
    "    \n",
    "    best_kill = np.max([data['kill_A'], data['kill_B']])\n",
    "    improve = kill_C - best_kill\n",
    "    improve_p = (kill_C - best_kill) / best_kill\n",
    "    \n",
    "    sum_kill_dif = np.sum(np.abs(cluster_kill_A - cluster_kill_B))\n",
    "    \n",
    "    ##### save output #####\n",
    "    \n",
    "    rows += [[kill_C, improve, improve_p, sum_kill_dif]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:12.748850Z",
     "start_time": "2020-07-11T15:52:12.724236Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1007</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>20.448897</td>\n",
       "      <td>9.551561</td>\n",
       "      <td>0.876504</td>\n",
       "      <td>0.177611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>133</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>59.126943</td>\n",
       "      <td>59.126943</td>\n",
       "      <td>63.508422</td>\n",
       "      <td>4.381480</td>\n",
       "      <td>0.074103</td>\n",
       "      <td>48.407217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>201</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>39.492534</td>\n",
       "      <td>39.492534</td>\n",
       "      <td>45.978769</td>\n",
       "      <td>6.486234</td>\n",
       "      <td>0.164240</td>\n",
       "      <td>28.772809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>1010</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>16.555392</td>\n",
       "      <td>16.555392</td>\n",
       "      <td>25.500425</td>\n",
       "      <td>8.945033</td>\n",
       "      <td>0.540309</td>\n",
       "      <td>5.835667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>182</td>\n",
       "      <td>1032</td>\n",
       "      <td>3.193168</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>46.369313</td>\n",
       "      <td>46.369313</td>\n",
       "      <td>52.118375</td>\n",
       "      <td>5.749063</td>\n",
       "      <td>0.123984</td>\n",
       "      <td>35.649587</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     delta     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1032  1007      1032  3.193168  10.719726    Afatinib          1   \n",
       "1   HN120  1032   133      1032  3.193168  10.719726    Afatinib          1   \n",
       "2   HN120  1032   201      1032  3.193168  10.719726    Afatinib          1   \n",
       "3   HN120  1032  1010      1032  3.193168  10.719726    Afatinib          1   \n",
       "4   HN120  1032   182      1032  3.193168  10.719726    Afatinib          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \\\n",
       "0       10.719726      1007           Docetaxel       10.897336  10.897336   \n",
       "1       10.719726       133         Doxorubicin       59.126943  59.126943   \n",
       "2       10.719726       201        Epothilone B       39.492534  39.492534   \n",
       "3       10.719726      1010           Gefitinib       16.555392  16.555392   \n",
       "4       10.719726       182  Obatoclax Mesylate       46.369313  46.369313   \n",
       "\n",
       "      kill_C   improve  improve_p  sum_kill_dif  \n",
       "0  20.448897  9.551561   0.876504      0.177611  \n",
       "1  63.508422  4.381480   0.074103     48.407217  \n",
       "2  45.978769  6.486234   0.164240     28.772809  \n",
       "3  25.500425  8.945033   0.540309      5.835667  \n",
       "4  52.118375  5.749063   0.123984     35.649587  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = pd.concat([drug_combi_pred_df, pd.DataFrame(rows, columns=['kill_C', 'improve', 'improve_p', 'sum_kill_dif'])], axis=1)\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:13.463314Z",
     "start_time": "2020-07-11T15:52:13.441976Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>10.897336</td>\n",
       "      <td>20.448897</td>\n",
       "      <td>9.551561</td>\n",
       "      <td>0.876504</td>\n",
       "      <td>0.177611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>59.126943</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>59.126943</td>\n",
       "      <td>63.508422</td>\n",
       "      <td>4.381480</td>\n",
       "      <td>0.074103</td>\n",
       "      <td>48.407217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>39.492534</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>39.492534</td>\n",
       "      <td>45.978769</td>\n",
       "      <td>6.486234</td>\n",
       "      <td>0.164240</td>\n",
       "      <td>28.772809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>16.555392</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>16.555392</td>\n",
       "      <td>25.500425</td>\n",
       "      <td>8.945033</td>\n",
       "      <td>0.540309</td>\n",
       "      <td>5.835667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1032</td>\n",
       "      <td>Afatinib</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>1</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>46.369313</td>\n",
       "      <td>10.719726</td>\n",
       "      <td>46.369313</td>\n",
       "      <td>52.118375</td>\n",
       "      <td>5.749063</td>\n",
       "      <td>0.123984</td>\n",
       "      <td>35.649587</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id_A drug_name_A drug_id_B         drug_name_B  cluster_p  \\\n",
       "0   HN120      1032    Afatinib      1007           Docetaxel          1   \n",
       "1   HN120      1032    Afatinib       133         Doxorubicin          1   \n",
       "2   HN120      1032    Afatinib       201        Epothilone B          1   \n",
       "3   HN120      1032    Afatinib      1010           Gefitinib          1   \n",
       "4   HN120      1032    Afatinib       182  Obatoclax Mesylate          1   \n",
       "\n",
       "   cluster_kill_A  cluster_kill_B     kill_A     kill_B     kill_C   improve  \\\n",
       "0       10.719726       10.897336  10.719726  10.897336  20.448897  9.551561   \n",
       "1       10.719726       59.126943  10.719726  59.126943  63.508422  4.381480   \n",
       "2       10.719726       39.492534  10.719726  39.492534  45.978769  6.486234   \n",
       "3       10.719726       16.555392  10.719726  16.555392  25.500425  8.945033   \n",
       "4       10.719726       46.369313  10.719726  46.369313  52.118375  5.749063   \n",
       "\n",
       "   improve_p  sum_kill_dif  \n",
       "0   0.876504      0.177611  \n",
       "1   0.074103     48.407217  \n",
       "2   0.164240     28.772809  \n",
       "3   0.540309      5.835667  \n",
       "4   0.123984     35.649587  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = drug_combi_pred_df[['patient', 'drug_id_A', 'drug_name_A', 'drug_id_B', 'drug_name_B', 'cluster_p', 'cluster_kill_A', 'cluster_kill_B', 'kill_A', 'kill_B', 'kill_C', 'improve', 'improve_p', 'sum_kill_dif']]\n",
    "\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-11T15:52:13.917092Z",
     "start_time": "2020-07-11T15:52:13.905960Z"
    }
   },
   "outputs": [],
   "source": [
    "if dosage_shifted:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}_shifted.csv'.format(ref_type, model_name), index=False)\n",
    "else:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}.csv'.format(ref_type, model_name), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
